﻿module Sustainalytics.GCCS {

    import Common = Sustainalytics.Common;

    export class GCPrincipleSummaryService implements IGCPrincipleSummaryService {

        constructor(
            private $q: ng.IQService,
            private $http: ng.IHttpService,
            private notificationFactory: Common.INotificationFactory,
            private staticObjects: Common.IStaticObjects) {
        }

        public get(companyId: string, overallAssessment: GCAssessment, fetchCount?: boolean): ng.IPromise<any> {
            var def = this.$q.defer();

            var args = $.param(
                {
                    companyId: companyId,
                    overallAssessment: overallAssessment,
                    fetchCount: fetchCount
                });

            var httpPromise: ng.IHttpPromise<any> = this.$http.get('/gccsserv/api/v1/gcprinciplesummary?' + args);

            httpPromise
                .success(function (data) {
                    def.resolve(data);
                })
                .error(function (data, status, headers, config) {
                    def.reject({ data: data, status: status, headers: headers, config: config });
                });

            return def.promise;
        }

        public getList(companyId: string, overallAssessment: GCAssessment): ng.IPromise<GCPrincipleSummaryDto[]> {
            var def = this.$q.defer();

            var promise = this.get(companyId, overallAssessment, false);
            return promise;
        }

        public getCount(companyId: string, overallAssessment: GCAssessment): ng.IPromise<number> {
            var def = this.$q.defer();

            var promise = this.get(companyId, overallAssessment, true);
            return promise;
        }
    }


    angular.module('Sustainalytics.GCCS').factory('GCPrincipleSummaryService',
        ($q: ng.IQService, $http: ng.IHttpService, notificationFactory: Common.INotificationFactory, staticObjects: Common.IStaticObjects) => {
            var service = new GCPrincipleSummaryService($q, $http, notificationFactory, staticObjects);
            return service;
        });
}